#include <iostream>
#include <cstdio>
#include <unistd.h>
#include <cstring>
using namespace std;
int map1[1000][1000][2];
int a, x, y, mapsize, sum = 0, times, moshi;
void moshi1();
int main()
{
	int t, msize, x, y, c, dituly;
	char mname[100], baocun;
	printf("地图来源： 1）手动输入 2）配置文件 ");
	cin >> dituly;
	//-------------------------------------------------------------------------------------------------------------------------
	if (dituly == 1)
	{
		printf("输入地图大小(10-1000) :　");
		scanf("%d", &msize);
		printf("输入演算步骤数 ： ");
		cin >> t;
		while (cin >> x >> y && (x != -1 || y != -1))
		{
			printf("x,y输入-1停止输入　\n");
			if (map1[x + 1][y + 1][0] == 0)
				map1[x + 1][y + 1][0] = 1;
			else
				map1[x + 1][y + 1][0] = 0;
			for (int i = 1; i < msize + 1; i++)
			{
				for (int j = 1; j < msize + 1; j++)
				{
					if (map1[i][j][0] == 1)
						printf("O ");
					else
						printf("- ");
				}
				printf("\n");
			}

			printf("\n\n");
		}

		cout << "1）查看模式 2）演示模式";
		cin >> moshi;
		if (moshi == 1)
		{
moshi1();
		}
		if (moshi == 2)
		{
			
moshi2();
		}
		else
		{
			cout << "1）查看模式 2）演示模式";
			cin >> moshi;
			if (moshi == 1)
			{
				freopen("in.txt", "r", stdin);
				cin >> mapsize;
				cin >> a;
				for (int i = 0; i < a; i++)
				{
					scanf("%d %d", &x, &y);
					map1[x + 1][y + 1][0] = 1;
				}

				cin >> times;
				cout << "The 0" << endl;
				if (mapsize < 10)
					for (int i = 0; i < mapsize + 1; i++)
						cout << i << " ";
				else
				{
					for (int i = 0; i < mapsize + 1; i++)
					{
						if (i < 10)
							cout << i << " ";
						else
							cout << i;
					}
				}
				cout << endl;
				for (int i = 1; i < mapsize + 1; i++)
				{
					if (i < 10)
						cout << i << " ";
					else
						cout << i;
					for (int j = 1; j < mapsize + 1; j++)
					{
						if (map1[i][j][0] == 1)
							printf("O ");
						else
							printf("%c ", '-');
						map1[i][j][1] = 0;
					}
					printf("\n");
				}
				for (int w = 0; w < times; w++)
				{
					for (int i = 1; i < mapsize + 1; i++)
					{
						for (int j = 1; j < mapsize + 1; j++)
						{
							if (map1[i - 1][j - 1][0] == 1)
								map1[i][j][1]++;
							if (map1[i][j - 1][0] == 1)
								map1[i][j][1]++;
							if (map1[i - 1][j][0] == 1)
								map1[i][j][1]++;
							if (map1[i + 1][j - 1][0] == 1)
								map1[i][j][1]++;
							if (map1[i - 1][j + 1][0] == 1)
								map1[i][j][1]++;
							if (map1[i + 1][j + 1][0] == 1)
								map1[i][j][1]++;
							if (map1[i + 1][j][0] == 1)
								map1[i][j][1]++;
							if (map1[i][j + 1][0] == 1)
								map1[i][j][1]++;
						}
					}

					for (int i = 1; i < mapsize + 1; i++)
					{
						for (int j = 1; j < mapsize + 1; j++)
						{
							if (map1[i][j][1] == 3)
								map1[i][j][0] = 1;
							if (map1[i][j][1] > 3)
								map1[i][j][0] = 0;
							if (map1[i][j][1] < 2)
								map1[i][j][0] = 0;
						}
					}
					printf("\n");
					cout << "The " << w + 1 << endl;
					if (mapsize < 10)
						for (int i = 0; i < mapsize + 1; i++)
							cout << i << " ";
					else
					{
						for (int i = 0; i < mapsize + 1; i++)
						{
							if (i < 10)
								cout << i << " ";
							else
								cout << i;
						}
					}
					cout << endl;
					for (int i = 1; i < mapsize + 1; i++)
					{
						if (i < 10)
							cout << i << " ";
						else
							cout << i;
						for (int j = 1; j < mapsize + 1; j++)
						{
							if (map1[i][j][0] == 1)
								printf("O ");
							else
								printf("%c ", '-');
							map1[i][j][1] = 0;
						}
						printf("\n");
					}
				}
			}
			if (moshi == 2)
			{
				cin >> mapsize;
				cin >> a;
				for (int i = 0; i < a; i++)
				{

					scanf("%d %d", &x, &y);
					map1[x + 1][y + 1][0] = 1;
				}

				scanf("%d", &times);
				system("clear");
				cout << "The 0" << endl;
				if (mapsize < 10)
					for (int i = 0; i < mapsize + 1; i++)
						cout << i << " ";
				else
				{
					for (int i = 0; i < mapsize + 1; i++)
					{
						if (i < 10)
							cout << i << " ";
						else
							cout << i;
					}
				}
				cout << endl;
				for (int i = 1; i < mapsize + 1; i++)
				{
					if (i < 10)
						cout << i << " ";
					else
						cout << i;
					for (int j = 1; j < mapsize + 1; j++)
					{
						if (map1[i][j][0] == 1)
							printf("O ");
						else
							printf("%c ", '-');
						map1[i][j][1] = 0;
					}
					printf("\n");
				}
				usleep(500000);
				system("clear");
				for (int w = 0; w < times; w++)
				{
					for (int i = 1; i < mapsize + 1; i++)
					{
						for (int j = 1; j < mapsize + 1; j++)
						{
							if (map1[i - 1][j - 1][0] == 1)
								map1[i][j][1]++;
							if (map1[i][j - 1][0] == 1)
								map1[i][j][1]++;
							if (map1[i - 1][j][0] == 1)
								map1[i][j][1]++;
							if (map1[i + 1][j - 1][0] == 1)
								map1[i][j][1]++;
							if (map1[i - 1][j + 1][0] == 1)
								map1[i][j][1]++;
							if (map1[i + 1][j + 1][0] == 1)
								map1[i][j][1]++;
							if (map1[i + 1][j][0] == 1)
								map1[i][j][1]++;
							if (map1[i][j + 1][0] == 1)
								map1[i][j][1]++;
						}
					}

					for (int i = 1; i < mapsize + 1; i++)
					{
						for (int j = 1; j < mapsize + 1; j++)
						{
							if (map1[i][j][1] == 3)
								map1[i][j][0] = 1;
							if (map1[i][j][1] > 3)
								map1[i][j][0] = 0;
							if (map1[i][j][1] < 2)
								map1[i][j][0] = 0;
						}
					}
					printf("\n");
					cout << "The " << w + 1 << endl;
					if (mapsize < 10)
						for (int i = 0; i < mapsize + 1; i++)
							cout << i << " ";
					else
					{
						for (int i = 0; i < mapsize + 1; i++)
						{
							if (i < 10)
								cout << i << " ";
							else
								cout << i;
						}
					}
					cout << endl;
					for (int i = 1; i < mapsize + 1; i++)
					{
						if (i < 10)
							cout << i << " ";
						else
							cout << i;
						for (int j = 1; j < mapsize + 1; j++)
						{
							if (map1[i][j][0] == 1)
								printf("O ");
							else
								printf("%c ", '-');
							map1[i][j][1] = 0;
						}
						printf("\n");
					}
					usleep(500000);

					if (w < times - 1)
						system("clear");
				}
			}
			fclose(stdin);
			return 0;
		}

		//---------------------------------------------------------------------------
		cout << "1）查看模式 2）演示模式";
		cin >> moshi;
		freopen("temp", "r", stdin);
		if (moshi == 1)
		{
moshi1();
		}
		if (moshi == 2)
		{
			cin >> mapsize;
			cin >> a;
			for (int i = 0; i < a; i++)
			{

				scanf("%d %d", &x, &y);
				map1[x + 1][y + 1][0] = 1;
			}

			scanf("%d", &times);
			system("clear");
			cout << "The 0" << endl;
			if (mapsize < 10)
				for (int i = 0; i < mapsize + 1; i++)
					cout << i << " ";
			else
			{
				for (int i = 0; i < mapsize + 1; i++)
				{
					if (i < 10)
						cout << i << " ";
					else
						cout << i;
				}
			}
			cout << endl;
			for (int i = 1; i < mapsize + 1; i++)
			{
				if (i < 10)
					cout << i << " ";
				else
					cout << i;
				for (int j = 1; j < mapsize + 1; j++)
				{
					if (map1[i][j][0] == 1)
						printf("O ");
					else
						printf("%c ", '-');
					map1[i][j][1] = 0;
				}
				printf("\n");
			}
			usleep(500000);
			system("clear");
			for (int w = 0; w < times; w++)
			{
				for (int i = 1; i < mapsize + 1; i++)
				{
					for (int j = 1; j < mapsize + 1; j++)
					{
						if (map1[i - 1][j - 1][0] == 1)
							map1[i][j][1]++;
						if (map1[i][j - 1][0] == 1)
							map1[i][j][1]++;
						if (map1[i - 1][j][0] == 1)
							map1[i][j][1]++;
						if (map1[i + 1][j - 1][0] == 1)
							map1[i][j][1]++;
						if (map1[i - 1][j + 1][0] == 1)
							map1[i][j][1]++;
						if (map1[i + 1][j + 1][0] == 1)
							map1[i][j][1]++;
						if (map1[i + 1][j][0] == 1)
							map1[i][j][1]++;
						if (map1[i][j + 1][0] == 1)
							map1[i][j][1]++;
					}
				}

				for (int i = 1; i < mapsize + 1; i++)
				{
					for (int j = 1; j < mapsize + 1; j++)
					{
						if (map1[i][j][1] == 3)
							map1[i][j][0] = 1;
						if (map1[i][j][1] > 3)
							map1[i][j][0] = 0;
						if (map1[i][j][1] < 2)
							map1[i][j][0] = 0;
					}
				}
				printf("\n");
				cout << "The " << w + 1 << endl;
				if (mapsize < 10)
					for (int i = 0; i < mapsize + 1; i++)
						cout << i << " ";
				else
				{
					for (int i = 0; i < mapsize + 1; i++)
					{
						if (i < 10)
							cout << i << " ";
						else
							cout << i;
					}
				}
				cout << endl;
				for (int i = 1; i < mapsize + 1; i++)
				{
					if (i < 10)
						cout << i << " ";
					else
						cout << i;
					for (int j = 1; j < mapsize + 1; j++)
					{
						if (map1[i][j][0] == 1)
							printf("O ");
						else
							printf("%c ", '-');
						map1[i][j][1] = 0;
					}
					printf("\n");
				}
				usleep(500000);

				if (w < times - 1)
					system("clear");
			}
		}
		fclose(stdin);

		//-------------------------SAVE------------------------------------
		/*
	printf("是否保存？y/n ");cin>>baocun;
	if(baocun=='y'){
	printf("输入地图名称：　");
	scanf("%s",&mname);
	freopen(mname,"w",stdout);
	for(int i=1;i<msize+1;i++){
		for(int j=1;j<msize+1;j++){
			if(map1[i+1][j+1][0]==1){
				c++;
			}
		}
	}			
	cout<<msize<<endl<<c<<endl;
	for(int i=1;i<msize+1;i++){
		for(int j=1;j<msize+1;j++){
			if(map1[i+1][j+1][0]==1){
				printf(" %d %d ",i,j);
			}
		}
	}
	cout<<endl<<t;
	}*/
		//-------------------------------------------------------------------
	}
	//-------------------------SAVE------------------------------------
	printf("是否保存地图？y/n ");
	cin >> baocun;
	if (baocun == 'y')
	{
		printf("输入地图名称：　");
		scanf("%s", &mname);
		freopen(mname, "w", stdout);
		for (int i = 1; i < msize + 1; i++)
		{
			for (int j = 1; j < msize + 1; j++)
			{
				if (map1[i + 1][j + 1][0] == 1)
				{
					c++;
				}
			}
		}
		cout << msize << endl
			<< c << endl;
		for (int i = 1; i < msize + 1; i++)
		{
			for (int j = 1; j < msize + 1; j++)
			{
				if (map1[i + 1][j + 1][0] == 1)
				{
					printf(" %d %d ", i, j);
				}
			}
		}
		cout << endl
			<< t << endl;

		/*if(baocun=='n'){
	freopen("temp","w",stdout);
	for(int i=1;i<msize+1;i++){
		for(int j=1;j<msize+1;j++){
			if(map1[i+1][j+1][0]==1){
				c++;
			}
		}
	}			
	cout<<msize<<endl<<c<<endl;
	for(int i=1;i<msize+1;i++){
		for(int j=1;j<msize+1;j++){
			if(map1[i+1][j+1][0]==1){
				printf(" %d %d ",i,j);
			}
		}
	}
	cout<<endl<<t<<endl;
	}*/
	}
	//-------------------------------------------------------------------
	return 0;
}
void moshi1(){
				cin >> mapsize;
			cin >> a;
			for (int i = 0; i < a; i++)
			{
				scanf("%d %d", &x, &y);
				map1[x + 1][y + 1][0] = 1;
			}

			cin >> times;
			cout << "The 0" << endl;
			if (mapsize < 10)
				for (int i = 0; i < mapsize + 1; i++)
					cout << i << " ";
			else
			{
				for (int i = 0; i < mapsize + 1; i++)
				{
					if (i < 10)
						cout << i << " ";
					else
						cout << i;
				}
			}
			cout << endl;
			for (int i = 1; i < mapsize + 1; i++)
			{
				if (i < 10)
					cout << i << " ";
				else
					cout << i;
				for (int j = 1; j < mapsize + 1; j++)
				{
					if (map1[i][j][0] == 1)
						printf("O ");
					else
						printf("%c ", '-');
					map1[i][j][1] = 0;
				}
				printf("\n");
			}
			for (int w = 0; w < times; w++)
			{
				for (int i = 1; i < mapsize + 1; i++)
				{
					for (int j = 1; j < mapsize + 1; j++)
					{
						if (map1[i - 1][j - 1][0] == 1)
							map1[i][j][1]++;
						if (map1[i][j - 1][0] == 1)
							map1[i][j][1]++;
						if (map1[i - 1][j][0] == 1)
							map1[i][j][1]++;
						if (map1[i + 1][j - 1][0] == 1)
							map1[i][j][1]++;
						if (map1[i - 1][j + 1][0] == 1)
							map1[i][j][1]++;
						if (map1[i + 1][j + 1][0] == 1)
							map1[i][j][1]++;
						if (map1[i + 1][j][0] == 1)
							map1[i][j][1]++;
						if (map1[i][j + 1][0] == 1)
							map1[i][j][1]++;
					}
				}

				for (int i = 1; i < mapsize + 1; i++)
				{
					for (int j = 1; j < mapsize + 1; j++)
					{
						if (map1[i][j][1] == 3)
							map1[i][j][0] = 1;
						if (map1[i][j][1] > 3)
							map1[i][j][0] = 0;
						if (map1[i][j][1] < 2)
							map1[i][j][0] = 0;
					}
				}
				printf("\n");
				cout << "The " << w + 1 << endl;
				if (mapsize < 10)
					for (int i = 0; i < mapsize + 1; i++)
						cout << i << " ";
				else
				{
					for (int i = 0; i < mapsize + 1; i++)
					{
						if (i < 10)
							cout << i << " ";
						else
							cout << i;
					}
				}
				cout << endl;
				for (int i = 1; i < mapsize + 1; i++)
				{
					if (i < 10)
						cout << i << " ";
					else
						cout << i;
					for (int j = 1; j < mapsize + 1; j++)
					{
						if (map1[i][j][0] == 1)
							printf("O ");
						else
							printf("%c ", '-');
						map1[i][j][1] = 0;
					}
					printf("\n");
				}
			}
}
void moshi2(){
							if (i < 10)
							cout << i << " ";
						else
							cout << i;
					}
				}
				cout << endl;
				for (int i = 1; i < mapsize + 1; i++)
				{
					if (i < 10)
						cout << i << " ";
					else
						cout << i;
					for (int j = 1; j < mapsize + 1; j++)
					{
						if (map1[i][j][0] == 1)
							printf("O ");
						elsecin >> mapsize;
			cin >> a;
			for (int i = 0; i < a; i++)
			{

				scanf("%d %d", &x, &y);
				map1[x + 1][y + 1][0] = 1;
			}

			scanf("%d", &times);
			system("clear");
			cout << "The 0" << endl;
			if (mapsize < 10)
				for (int i = 0; i < mapsize + 1; i++)
					cout << i << " ";
			else
			{
				for (int i = 0; i < mapsize + 1; i++)
				{
					if (i < 10)
						cout << i << " ";
					else
						cout << i;
				}
			}
			cout << endl;
			for (int i = 1; i < mapsize + 1; i++)
			{
				if (i < 10)
					cout << i << " ";
				else
					cout << i;
				for (int j = 1; j < mapsize + 1; j++)
				{
					if (map1[i][j][0] == 1)
						printf("O ");
					else
						printf("%c ", '-');
					map1[i][j][1] = 0;
				}
				printf("\n");
			}
			usleep(500000);
			system("clear");
			for (int w = 0; w < times; w++)
			{
				for (int i = 1; i < mapsize + 1; i++)
				{
					for (int j = 1; j < mapsize + 1; j++)
					{
						if (map1[i - 1][j - 1][0] == 1)
							map1[i][j][1]++;
						if (map1[i][j - 1][0] == 1)
							map1[i][j][1]++;
						if (map1[i - 1][j][0] == 1)
							map1[i][j][1]++;
						if (map1[i + 1][j - 1][0] == 1)
							map1[i][j][1]++;
						if (map1[i - 1][j + 1][0] == 1)
							map1[i][j][1]++;
						if (map1[i + 1][j + 1][0] == 1)
							map1[i][j][1]++;
						if (map1[i + 1][j][0] == 1)
							map1[i][j][1]++;
						if (map1[i][j + 1][0] == 1)
							map1[i][j][1]++;
					}
				}

				for (int i = 1; i < mapsize + 1; i++)
				{
					for (int j = 1; j < mapsize + 1; j++)
					{
						if (map1[i][j][1] == 3)
							map1[i][j][0] = 1;
						if (map1[i][j][1] > 3)
							map1[i][j][0] = 0;
						if (map1[i][j][1] < 2)
							map1[i][j][0] = 0;
					}
				}
				printf("\n");
				cout << "The " << w + 1 << endl;
				if (mapsize < 10)
					for (int i = 0; i < mapsize + 1; i++)
						cout << i << " ";
				else
				{
					for (int i = 0; i < mapsize + 1; i++)
					{
							printf("%c ", '-');
						map1[i][j][1] = 0;
					}
					printf("\n");
				}
				usleep(500000);

				if (w < times - 1)
					system("clear");
			}
}
